package com.lab.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lab.dao.CollegeDao;
import com.lab.po.College;
import com.lab.util.MySqlSessionFactory;

public class CollegeDaoImpl implements CollegeDao{
	//查询所有学院
	@Override
	public List<College> queryCollege() {
		SqlSession session=null;
		List<College> collegeList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			collegeList=session.selectList("CollegeXml.queryCollege");
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return collegeList;
	}

	@Override
	public int queryCollegeByName(String col_name) {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("CollegeXml.queryCollegeByName",col_name);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return i;
	}

	@Override
	public int addCollege(Map map) {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.insert("CollegeXml.addCollege",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return i;
	}

	@Override
	public int queryMaxCol_id() {
		SqlSession session=null;
		int i=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.selectOne("CollegeXml.queryMaxCol_id");
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return i;
	}
	
	//更新学院
	@Override
	public int updateCollege(Map map) {
		int i=0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();		
			i = session.update("CollegeXml.updateCollege",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}
	
	//删除学院
	@Override
	public int deleteCollege(int col_id) {
		int i = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			i=session.delete("CollegeXml.deleteCollege", col_id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}
	
	//根据ID查询学院名称
	@Override
	public String queryCollegeById(int col_id) {
		SqlSession session=null;
		String col_name=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			col_name=session.selectOne("CollegeXml.queryCollegeById",col_id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}		
		return col_name;
	}

	//根据名称查询学院id
	@Override
	public int queryCol_id(String col_name) {
		int i = 0;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			System.out.println(col_name);
			i=session.selectOne("CollegeXml.queryCol_id", col_name);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}

}
